package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.bo.CustomerInfoBO;
import com.mhc.suzuki.bo.EmergencyContactBO;
import com.mhc.suzuki.dal.model.GuaranteeDO;
import com.mhc.suzuki.dal.query.GuaranteeQuery;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.manager.GuaranteeManager;
import com.mhc.suzuki.model.bo.OrderListBO;
import com.mhc.suzuki.service.GuaranteeService;
import com.mhc.suzuki.service.OrderService;
import com.mhc.suzuki.util.BeanCopierUtil;
import com.mhc.suzuki.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class GuaranteeServiceImpl implements GuaranteeService {

    @Autowired
    private GuaranteeManager guaranteeManager;

    @Autowired
    private OrderService orderService;

    @Override
    public Boolean addGuarantee(GuaranteeDO guaranteeDO) throws SuzukiCommonException {
        return guaranteeManager.insertSelective(guaranteeDO) == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public GuaranteeDetailVO getGuaranteeDetailInfo(String idCard, String orderNo) throws SuzukiCommonException {
        CustomerInfoBO customerInfoBO = orderService.queryCustomerInfo(orderNo);
        GuaranteeDetailVO guaranteeDetailVO = new GuaranteeDetailVO();
        CustomerInfoVO customerInfoVO = new CustomerInfoVO();
        CustomerVO customer = new CustomerVO();
        HouseInfoVO houseInfo = new HouseInfoVO();
        List<EmergencyContactVO> emergencyContacts = new ArrayList<>();
        BeanCopierUtil.copy(customerInfoBO.getCustomer(), customer);
        BeanCopierUtil.copy(customerInfoBO.getHouseInfo(), houseInfo);
        List<EmergencyContactBO> emergencyContactBOList = customerInfoBO.getEmergencyContacts();
        emergencyContactBOList.forEach(emergencyContactBO -> {
            EmergencyContactVO emergencyContactVO = new EmergencyContactVO();
            BeanCopierUtil.copy(emergencyContactBO, emergencyContactVO);
            emergencyContacts.add(emergencyContactVO);
        });
        //紧急联系人
        customerInfoVO.setEmergencyContacts(emergencyContacts);
        //个人信息
        customerInfoVO.setCustomer(customer);
        //房产信息
        customerInfoVO.setHouseInfo(houseInfo);
        guaranteeDetailVO.setCustomerInfoVO(customerInfoVO);

        GuaranteeQuery guaranteeQuery = new GuaranteeQuery();
        guaranteeQuery.createCriteria().andIdCardEqualTo(idCard);
        List<GuaranteeDO> guaranteeDOList = guaranteeManager.selectByQuery(guaranteeQuery);
        List<GuaranteeVO> guaranteeVOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(guaranteeDOList)) {
            guaranteeDOList.forEach(guaranteeDO -> {
                GuaranteeVO guaranteeVO = BeanCopierUtil.convert(guaranteeDO, GuaranteeVO.class);
                guaranteeVOList.add(guaranteeVO);
            });
        }
        guaranteeDetailVO.setGuaranteeVOList(guaranteeVOList);
        OrderListBO orderListBO = new OrderListBO();
        orderListBO.setCustomerIdCard(idCard);
        orderListBO.setPageNO(1);
        orderListBO.setPageSize(10);
        guaranteeDetailVO.setOrderInfoVOS(orderService.queryOrderList(orderListBO).getResult());
        return guaranteeDetailVO;
    }
}
